using System;
using System.Collections.Generic;
using Pinwheel.LSerialize.Formatters;

namespace Pinwheel.LSerialize
{
    public static partial class LFormatterProvider
    {
        private static readonly Dictionary<Type, Type> CollectionFormatters = new Dictionary<Type, Type>(2)
        {
            { typeof(List<>), typeof(ListFormatter<>) },
            { typeof(Dictionary<,>), typeof(DictionaryFormatter<,>) },
        };
    }
}

namespace Pinwheel.LSerialize.Formatters
{
    [Preserve]
    public sealed unsafe class ListFormatter<T> : LFormatter<List<T>> where T : IEquatable<T>
    {
        [Preserve]
        public override void Serialize(LSerializeContext* context, LStreamWriter* writer, ref List<T> value)
        {
            Profiler.BeginSample("ListFormatter.Serialize");
            writer->WriteListField(context, value);
            Profiler.EndSample();
        }
    
        [Preserve]
        public override void Deserialize(LSerializeContext* context, LStreamReader* reader, ref List<T> value)
        {
            Profiler.BeginSample("ListFormatter.Deserialize");
            reader->ReadListField(context, ref value);
            Profiler.EndSample();
        }
    }
    
    
    [Preserve]
    public sealed unsafe class DictionaryFormatter<TKey, TValue> : LFormatter<Dictionary<TKey, TValue>> 
        where TKey : notnull, IEquatable<TKey>
        where TValue : IEquatable<TValue>
    {
        [Preserve]
        public override void Serialize(LSerializeContext* context, LStreamWriter* writer, ref Dictionary<TKey, TValue> value)
        {
            Profiler.BeginSample("DictionaryFormatter.Serialize");
            writer->WriteDictionaryField(context, value);
            Profiler.EndSample();
        }
    
        [Preserve]
        public override void Deserialize(LSerializeContext* context, LStreamReader* reader, ref Dictionary<TKey, TValue> value)
        {
            Profiler.BeginSample("DictionaryFormatter.Deserialize");
            reader->ReadDictionaryField(context, ref value);
            Profiler.EndSample();
        }
    }
}

